﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace furion.blog.Utils
{
    public class EnumUtil
    {
        /// <summary>
        /// 获取自定义attribute 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumObj"></param>
        /// <returns></returns>
        public static T GetAttribute<T>(Enum enumObj) where T : Attribute
        {
            Type type = enumObj.GetType();
            Attribute attr = null;
            try
            {
                String enumName = Enum.GetName(type, enumObj);  //获取对应的枚举名
                FieldInfo field = type.GetField(enumName);
                attr = field.GetCustomAttribute(typeof(T), false);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                //       throw ex;
            }

            return (T)attr;
        }

    }
}
